import React from 'react';
import { connect } from 'dva';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import BatchUpload from '../../components/BatchUpload/Index';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';

@connect(({ card, loading, card: { fileId, fileName } }) => ({
  uploading: loading.effects['card/uploadFileWithProgress'],
  card,
  fileId,
  fileName,
}))
class Batch extends React.PureComponent {
  componentDidMount() {
    this.clearModel();
  }
  
  componentWillUnmount() {
    this.clearModel();
  }

  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'card/updateModelData',
      payload: {
        currentStep: 0,
        progressPercent: 0,
        showProgress: false,
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'card/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      this.clearModel();
    }
  };

  // 获取
  componentDidMount = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'card/fetchTemplate',
      payload: {
        tempSerType: 9, // 批量新增
      },
    });
  };

  // 下一步
  onNext = () => {
    const {
      dispatch,
      form: { validateFields },
      card: { currentStep },
    } = this.props;
    validateFields((err, values) => {
      if (!err) {
        dispatch({
          type: 'addemp/getDownloadInfo',
          payload: {
            ...values,
          },
        });
      }
    });
    this.setCurrentstep(currentStep + 1);
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const { dispatch } = this.props;

    dispatch({
      type: 'card/uploadFileWithProgress',
      payload: { formData, tag: 'ab' },
    }).then(res => {
      if (res.success) {
        const {
          card: { resultInfo },
        } = this.props;
        dispatch({
          type: 'card/updateModelData',
          payload: {
            showProgress: true,
            resultInfo: {
              ...resultInfo,
              errorUrl: '/esApi/insCardHdl/downloadExcelErrData', // 下载错误信息地址
              errorParams: null, // 下载错误数据参数
            },
          },
        });
      }
    });
  };

  render() {
    const {
      fileId,
      fileName,
      uploading,
      card: { progressPercent, currentStep, resultInfo, showProgress },
    } = this.props;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <FileUpload
            title="上传文件"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: {
                fileId,
                fileName,
              },
              url: '/esApi/fileServer/downloadFile',
              type: 'GET',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
            // 展示进度条需要的数据
            showProgress={showProgress}
            progressPercent={progressPercent || 0}
            progressTitle="批量新增"
            progressVisible={showProgress}
            handleModalVisible={this.handleModalVisible}
          />
          <Finish title="完成" resultInfo={resultInfo} setCurrentstep={this.setCurrentstep} />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}

export default Batch;
